package com.xszx.controller;

import com.github.pagehelper.PageInfo;
import com.xszx.config.PageRequest;
import com.xszx.entity.Client;
import com.xszx.entity.Cost;
import com.xszx.service.CostService;
import com.xszx.vo.AjaxJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/cost")
public class CostController {

    @Autowired
    private CostService costService;


    @RequestMapping("/costList")
    public AjaxJson costList(PageRequest pageRequest,
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer size){
        pageRequest.setPage(page);
        pageRequest.setSize(size);
        AjaxJson ajaxJson = new AjaxJson();
        try {
            PageInfo<Cost> costList = costService.getCostListInfo(pageRequest);
            System.out.println("costList:"+costList);
            ajaxJson.put("costList",costList);
        }  catch (Exception e) {
            ajaxJson.setSuccess("false");
            ajaxJson.setErrMsg("获取费用列表失败");
            e.printStackTrace();
        }
        return ajaxJson;
    }

    @RequestMapping("/addCost")
    public AjaxJson addCost(@RequestBody Cost cost){
        AjaxJson ajaxJson = new AjaxJson();
        try {
            costService.addCost(cost);
            ajaxJson.setErrMsg("添加费用成功");
        }  catch (Exception e) {
            ajaxJson.setSuccess("false");
            ajaxJson.setErrMsg("添加费用失败");
            e.printStackTrace();
        }
        return ajaxJson;
    }
    @RequestMapping("/updateCost")
    public AjaxJson updateCost(@RequestBody Cost cost){
        System.out.println("cost:"+cost);
        AjaxJson ajaxJson = new AjaxJson();
        try {
            costService.updateCost(cost);
            ajaxJson.setErrMsg("修改费用成功");
        }  catch (Exception e) {
            ajaxJson.setSuccess("false");
            ajaxJson.setErrMsg("修改费用失败");
            e.printStackTrace();
        }
        return ajaxJson;
    }

    @RequestMapping("/deleteCost")
    public AjaxJson deleteCost(Integer id){
        AjaxJson ajaxJson = new AjaxJson();
        try {
            costService.deletCost(id);
            ajaxJson.setErrMsg("删除费用成功");
        }  catch (Exception e) {
            ajaxJson.setSuccess("false");
            ajaxJson.setErrMsg("删除费用失败");
            e.printStackTrace();
        }
        return ajaxJson;
    }


}
